const getroletypes = async() => {
    let keyword = document.querySelector('.search').value
    let keyurl;
    if (keyword && keyword) {
        keyurl = `getroletypes?pagenum=1&pagesize=10&keyword=${keyword}`;
    } else {
        keyurl = `getroletypes?pagenum=1&pagesize=10`;
    }
    let response = await ajax(keyurl)
    let tbody = document.querySelector('tbody');
    let roletypes = response.data
    tbody.innerHTML = ''
    for (const roletype of roletypes) {
        let tr = document.createElement('tr');
        tr.innerHTML = `
                        <td>${roletype.id}</td>
                        <td>${roletype.roletypename}</td>
                        <td>${roletype.description?roletype.description:'未填写'}</td>
                        <td>
                            <button class="update" onclick="to_roletype_update(${roletype.id})">编辑</button>
                            <button class="deletebutton" onclick="delete_roletype(${roletype.id})">删除</button>
                        </td>
                        `
        tbody.appendChild(tr)
    }
}
const to_roletype_update = (_uid) => {
    location.href = `./roletype_update.html?id=${_uid}`
}

const delete_roletype = (uid) => {
    let response = ajax(`deleteroletype?id=${uid}`, 'DELETE')
    location.href = './roletype.html'
    alert('删除角色类型成功')
}
window.onload = function() {
    getroletypes()
}